package com.ycsj.goldmedalnewconcept.parent.activity;

import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.mob.MobSDK;
import com.ycsj.common.base.BaseActivity;
import com.ycsj.common.base.BaseApplication;
import com.ycsj.common.base.BusInfo;
import com.ycsj.common.manager.BannerManager;
import com.ycsj.common.manager.CustomDialogManager;
import com.ycsj.common.manager.PickChooseManager;
import com.ycsj.common.manager.PopUpManager;
import com.ycsj.common.utils.GsonUtil;
import com.ycsj.common.utils.UiUtils;
import com.ycsj.goldmedalnewconcept.R;
import com.ycsj.goldmedalnewconcept.common.Constants;
import com.ycsj.goldmedalnewconcept.common.HttpAddress;
import com.ycsj.goldmedalnewconcept.common.bean.EventInfo;
import com.ycsj.goldmedalnewconcept.common.bean.ResultInfo;
import com.ycsj.goldmedalnewconcept.common.http.HttpUtils;
import com.ycsj.goldmedalnewconcept.common.util.AccountUtils;
import com.youth.banner.Banner;
import com.youth.banner.Transformer;
import com.youth.banner.listener.OnBannerListener;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.onekeyshare.OnekeyShare;
import cn.sharesdk.onekeyshare.ShareContentCustomizeCallback;
import cn.sharesdk.sina.weibo.SinaWeibo;
import okhttp3.Call;

/**
 * 活动详情
 * Created by Administrator on 2018/7/13 0013.
 */

public class ParentsEventDetailsActivity extends BaseActivity implements View.OnClickListener {

    @BindView(R.id.tv_title)
    TextView tvTitle;

    @BindView(R.id.banner)
    Banner banner;
    @BindView(R.id.tv_num)
    TextView tvNum;

    //活动名称
    @BindView(R.id.tv_name)
    TextView tvName;
    // 新概念1班,新概念2班
    @BindView(R.id.tv_target)
    TextView tvTarget;

    //时间
    @BindView(R.id.tv_time)
    TextView tvTime;

    //地址
    @BindView(R.id.tv_address)
    TextView tvAddress;
    //活动内容
    @BindView(R.id.tv_content)
    TextView tvContent;

    //查看报名名单
    @BindView(R.id.bt_look_people)
    Button btLookPeople;

    @BindView(R.id.ll_conent)
    LinearLayout llConent;

    @BindView(R.id.rl_loading)
    RelativeLayout rlLoading;

    //分享
    @BindView(R.id.lv_edit)
    ImageView lv_edit;


    //页面传递的对象
    private EventInfo.ListBean info;


    //报名活动的请求参数
    private String acc, role, apptoken;

    @Override
    protected int getContentLayout() {
        return R.layout.parent_activity_event_details;
    }

    @Override
    protected void init() {

        //初始化状态栏
        initStateBar();

        //获取Intent对象
        initIntent();

        //初始化报名请求的参数
        initParams();

        //设置页面其它的数据
        setPageData();

    }


    /**
     * 获取Intent对象
     */
    private void initIntent() {
        info = (EventInfo.ListBean) getIntent().getSerializableExtra("data");
    }

    /**
     * 初始化报名请求的参数
     */
    private void initParams() {
        acc = AccountUtils.getAccount(this);
        role = AccountUtils.getRole(this);
        apptoken = AccountUtils.getToken(this);
    }

    /**
     * 设置页面其它显示数据
     */
    private void setPageData() {

        tvTitle.setText("活动报名");

        rlLoading.setVisibility(View.GONE);

        llConent.setVisibility(View.VISIBLE);

        if (info != null) {

            //设置轮播图的数据
            setBanner();

            //轮播图的标题
            tvName.setText(info.getTag());

            //报名人数
            if (TextUtils.isEmpty(info.getCount())) {
                tvNum.setText("0");
            } else {
                tvNum.setText(info.getCount());
            }

            //活动对象
            tvTarget.setText(info.getTarget());

            //活动时间
            tvTime.setText(info.getBegintime() + "  至  " + info.getEndtime());

            //活动地点
            tvAddress.setText(info.getAddress());

            //活动简介
            tvContent.setText(info.getContent());

            //根据是否有人报名来设置报名按钮的背景以及是否可点击的状态
            if ("0".equals(info.getIsEnroll())) {
                //未报名
                if ("1".equals(info.getStatus())) {
                    //已结束
                    btLookPeople.setText("已结束");
                    btLookPeople.setBackgroundResource(R.drawable.parent_bt_nolook_people);
                    btLookPeople.setEnabled(false);
                } else {
                    btLookPeople.setText("我要报名");
                    btLookPeople.setBackgroundResource(R.drawable.parent_bt_look_people);
                    btLookPeople.setEnabled(true);
                }

            } else if ("1".equals(info.getIsEnroll())) {
                btLookPeople.setText("已报名");
                btLookPeople.setBackgroundResource(R.drawable.parent_bt_nolook_people);
                btLookPeople.setEnabled(false);
            }
        }
    }


    /**
     * 设置轮播图的数据
     */
    private void setBanner() {
        if (info != null) {
            final List<String> bannerUrls = new ArrayList<>();
            if (!TextUtils.isEmpty(info.getPicture1())) {
                bannerUrls.add(info.getPicture1());
            }
            if (!TextUtils.isEmpty(info.getPicture2())) {
                bannerUrls.add(info.getPicture2());
            }

            if (!TextUtils.isEmpty(info.getPicture3())) {
                bannerUrls.add(info.getPicture3());
            }

            if (!TextUtils.isEmpty(info.getPicture4())) {
                bannerUrls.add(info.getPicture4());
            }
            if (!TextUtils.isEmpty(info.getPicture5())) {
                bannerUrls.add(info.getPicture5());
            }
            if (!TextUtils.isEmpty(info.getPicture6())) {
                bannerUrls.add(info.getPicture6());
            }
            if (!TextUtils.isEmpty(info.getPicture7())) {
                bannerUrls.add(info.getPicture7());
            }
            if (!TextUtils.isEmpty(info.getPicture8())) {
                bannerUrls.add(info.getPicture8());
            }
            if (!TextUtils.isEmpty(info.getPicture9())) {
                bannerUrls.add(info.getPicture9());
            }

            if (bannerUrls.size() == 0) {
                List<Integer> imgs = new ArrayList<>();
                imgs.add(R.drawable.base_event_def);
                //证明没有上传图片,那么现实默认的本地图片
                BannerManager.setDataLocal(banner, imgs, Transformer.ZoomIn);
            } else {
                BannerManager.setData(banner, true, bannerUrls, Transformer.ZoomIn);
                //设置点击事件
                banner.setOnBannerListener(new OnBannerListener() {
                    @Override
                    public void OnBannerClick(int position) {
                        PickChooseManager.startShowBigActivity(ParentsEventDetailsActivity.this, bannerUrls, position);
                    }
                });
            }
        }
    }


    /**
     * 点击事件
     *
     * @param v
     */
    @OnClick({R.id.iv_back, R.id.bt_look_people, R.id.lv_edit})
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.iv_back) {
            //返回
            finish();
        } else if (id == R.id.bt_look_people) {
            //我要报名,弹框提示是否需要报名
            showDialog();
        } else if (id == R.id.lv_edit) {
            share();
        }
    }


    /**
     * 分享活动
     */
    private void share() {
        MobSDK.init(this, "12c24ea0b59b0",
                "aae63565fbd69b1acbfcdd0d5ac0b35c");
        OnekeyShare oks = new OnekeyShare();
        // 关闭sso授权
        oks.disableSSOWhenAuthorize();
        oks.setShareContentCustomizeCallback(new ShareContentCustomizeCallback() {

            @Override
            public void onShare(Platform platform, Platform.ShareParams paramsToShare) {
                String title = "来自"
                        + AccountUtils.getSchoolName(ParentsEventDetailsActivity.this)
                        + "学校的活动";
                if (SinaWeibo.NAME.equals(platform.getName())) {
                    paramsToShare.setTitle(getString(R.string.share));
                    paramsToShare.setTitleUrl(HttpAddress.EVENTSHARE
                            + "/JPXB/sharePage/pages/JPXGN/activePage/activePage.html?rowId="
                            + info.getRowId() + "&school=" + AccountUtils.getSchoolId(ParentsEventDetailsActivity.this));
                    paramsToShare.setTitle(title);
                    paramsToShare.setText(title);
                    paramsToShare
                            .setImageUrl("http://139.129.57.83:8080/Appandroid/applogo.png");
                    paramsToShare.setUrl(HttpAddress.EVENTSHARE
                            + "/JPXB/sharePage/pages/JPXGN/activePage/activePage.html?rowId="
                            + info.getRowId() + "&school=" + AccountUtils.getSchoolId(ParentsEventDetailsActivity.this));
                    paramsToShare.setComment("");
                    paramsToShare.setSite("金牌君");
                    paramsToShare.setSiteUrl(HttpAddress.EVENTSHARE
                            + "/JPXB/sharePage/pages/JPXGN/activePage/activePage.html?rowId="
                            + info.getRowId() + "&school=" + AccountUtils.getSchoolId(ParentsEventDetailsActivity.this));
                } else {
                    paramsToShare.setShareType(Platform.SHARE_WEBPAGE);
                    paramsToShare.setTitle(getString(R.string.share));
                    paramsToShare.setTitleUrl(HttpAddress.EVENTSHARE
                            + "/JPXB/sharePage/pages/JPXGN/activePage/activePage.html?rowId="
                            + info.getRowId() + "&school=" + AccountUtils.getSchoolId(ParentsEventDetailsActivity.this));
                    paramsToShare.setTitle(title);
                    paramsToShare.setText(title);
                    paramsToShare
                            .setImageUrl("http://139.129.57.83:8080/Appandroid/applogo.png");

                    paramsToShare.setUrl(HttpAddress.EVENTSHARE
                            + "/JPXB/sharePage/pages/JPXGN/activePage/activePage.html?rowId="
                            + info.getRowId() + "&school=" + AccountUtils.getSchoolId(ParentsEventDetailsActivity.this));
                    paramsToShare.setComment("");
                    paramsToShare.setSite("金牌君");
                    paramsToShare.setSiteUrl(HttpAddress.EVENTSHARE
                            + "/JPXB/sharePage/pages/JPXGN/activePage/activePage.html?rowId="
                            + info.getRowId() + "&school=" + AccountUtils.getSchoolId(ParentsEventDetailsActivity.this));
                }
            }
        });
        oks.show(this);
    }


    /**
     * 弹框
     */
    private void showDialog() {

        PopUpManager.showPopByMatchParent(this, R.layout.pop_common_delete, 0.3f, this, new PopUpManager.onGetViewListener() {
            @Override
            public void getChildView(View view, int layoutResId) {
                TextView tvMsg = view.findViewById(R.id.txt_msg);
                tvMsg.setText("是否确定参加此次活动?");

                //取消按钮
                Button btCancel = view.findViewById(R.id.btn_neg);
                btCancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        PopUpManager.dismiss();
                    }
                });

                //确定按钮
                Button btOk = view.findViewById(R.id.btn_pos);

                btOk.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        PopUpManager.dismiss();
                        doApplyAction();
                    }
                });
            }
        });
    }

    /**
     * 报名的操作
     */
    private void doApplyAction() {
        CustomDialogManager.show(this, "报名中...");
        Map<String, String> map = new HashMap<>();
        map.put("type", "6");
        map.put("acc", acc);
        map.put("role", role);
        map.put("rowid2", info.getRowId() + "");
        map.put("apptoken", apptoken);
        map.put("platform", "2");

        HttpUtils.post(HttpAddress.PARENTEVENTBAOMING, map, this, new HttpUtils.CallBack() {
            @Override
            public void onError(Call call, Exception e, int id) {
                CustomDialogManager.dissmiss();
                UiUtils.Toast("报名失败" + BaseApplication.ERROR, false);
            }

            @Override
            public void onResponse(String response, int id) {
                CustomDialogManager.dissmiss();
                if (!TextUtils.isEmpty(response)) {
                    ResultInfo resinfo = null;
                    try {
                        resinfo = GsonUtil.jsonToBean(response, ResultInfo.class);
                    } catch (Exception e) {
                        Log.i("test", Constants.PARSEDATAERROR);
                    }
                    if (null != resinfo) {
                        if ("200".equals(resinfo.state)) {
                            ToastUtils.showShort("报名成功");
                            BusInfo busInfo = new BusInfo();
                            busInfo.type = ParentEventActivity.UPDATE;
                            EventBus.getDefault().post(busInfo);
                            // 关闭当前页面
                            finish();
                        } else {
                            ToastUtils.showShort("报名失败" + Constants.ERRORSTATE + resinfo.state);
                            Log.i("test", Constants.ERRORSTATE + resinfo.state);
                        }

                    } else {
                        Log.i("test", Constants.EMPTY);
                    }

                } else {
                    Log.i("test", Constants.EMPTY);
                }
            }
        });

    }
}
